<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Web extends CI_Controller
{
	var $data		= array();
	public function __construct()
	{
		parent::__construct();
	}
	
	public function index()
	{
		$this->output->enable_profiler(false);
		$this->load->model("web/mod_general");
		$this->load->model("web/mod_main");
		$this->load->library('session');

		// get banner sizes
		$this->data["Template"]			= $this->config->item('base_url')."application/views/template/";
		$this->data["TemplateEngine"]			= $this->config->item('base_url')."application/views/engine/template/";
		
		
		$this->data["baseUrl"]			= $this->config->item('base_url');
		$this->data["baseUrlSave"]		= $this->config->item('base_url')."?";
		$this->head["baseUrl"]			= $this->config->item('base_url');
		$this->head["baseUrlSave"]		= $this->config->item('base_url')."?";
		$this->head["engineTemplate"]	= $this->config->item('base_url')."application/views/engine/template/";
		$this->data["template"]			= $this->config->item('base_url')."application/views/template/";
		$theme_name			= $this->mod_general->get_template_name($this->data["template"]."theme_name.txt");
		$theme_path			= $this->data["template"].$theme_name."/";
		$theme_path_view	= "template/".$theme_name."/";
		$this->data["template_name"]	= $theme_name;
		$this->head["template_path"]	= $theme_path;
		$this->head["template_view"]	= $theme_path_view;
		$this->data["template_view"]	= $theme_path_view;
		
		$this->head['web_title']		= $this->mod_general->get_web_setting('web_title');
		$this->head['nama_sekolah']		= $this->mod_general->get_web_setting('front_title');
		$this->head['moto_sekolah']		= $this->mod_general->get_web_setting('moto_sekolah');
		$this->head['logo_sekolah']		= $this->mod_general->get_web_setting('web_logo');
		$this->head['moto_sekolah']		= $this->mod_general->get_web_setting('moto_sekolah');
		$this->head['alamat_sekolah']	= $this->mod_general->get_web_setting('alamat_sekolah');
		$this->head['telp_sekolah']		= $this->mod_general->get_web_setting('telp_sekolah');
		
		$this->data['banner_bawah']		= $this->mod_general->get_web_setting('banner_bawah');
		$this->data['banner_bawah']		= $this->mod_main->selipin_name($this->data['banner_bawah'], "resize");
		
		$mod		= $this->uri->segment(1);
		$this->head["menu_main"]		= $this->mod_main->get_menu_main();
		$this->data['sys_page']			= "detail";		
		$this->head['sys_page']			= "detail";		
		
		$this->data["galeri_depan"]		= $this->mod_main->get_galeri_depan();
		$this->head["galeri_depan"]		= $this->data["galeri_depan"];

		$this->data["berita"]			= $this->mod_main->get_content_short(1, 5);
		$this->data["info"]				= $this->mod_main->get_content_short(2, 6);
		$this->data["extra"]			= $this->mod_main->get_content_short(3, 3);
		$this->data["banner_sekolah"]	= $this->mod_main->get_banner_sekolah();
		$this->data["banner_inolabs"]	= $this->mod_main->get_inolabs_banner();
		
		$this->head["header_title"]		= $this->mod_general->get_web_setting('web_title');
		$this->head["header_desc"]		= $this->mod_general->get_web_setting('web_desc');
		$this->head["header_keyword"]	= $this->mod_general->get_web_setting('web_keyword');
		
		
		$id_data						= $this->uri->segment(2);
		
		$this->loginAction();
		$this->cekLogin();
		
		switch($mod)
		{
			case "detail":
				$module	= "content_detail";
				$id_ct				= $this->uri->segment(2);
				if(is_numeric($id_ct))
				{
					$this->data['content_d']		=	$this->mod_main->get_content_full($id_ct);
					$this->head["header_title"]		= 	$this->data['content_d']['judul'];
					$this->head["header_desc"]		= 	$this->data['content_d']['meta_desc'];
					$this->head["header_keyword"]	= 	$this->data['content_d']['keyword'];
				} 
			break;
			case "materi":
				$module	= "materi_detail";
				$id_ct				= $this->uri->segment(2);
				if(is_numeric($id_ct))
				{
					$this->data['content_d']		=	$this->mod_main->get_materi_full($id_ct);
					$this->head["header_title"]		= 	$this->data['content_d']['judul'];
					$this->data['nama_mp']			= 	$this->mod_main->get_nama_mp($this->data['content_d']['kode_mata_kuliah']);
				} 
			break;
			case "page":
				$module	= "page";
				$id_ct				= $this->uri->segment(2);
				if(is_numeric($id_ct))
				{
					$this->data['page']				=	$this->mod_main->get_page_full($id_ct);
					$_web_title						= 	$this->data['page']['title'];
					$this->head["header_title"]		= $this->mod_general->get_web_setting('web_title')." - ".$_web_title;
				}
			break; 
			case "kontak":

				$module	= "kontak";
				$nama 	= $this->input->post("nama");
				$email 	= $this->input->post("email");
				$subjek = $this->input->post("subjek");
				$isi 	= $this->input->post("isi");
				$init 	= $this->input->post("init");
				if($init!="")
				{
					$hasil =  
							$this->mod_main->insert_kontak_kami
							(
								$nama, 
								$email,
								$subjek,
								$isi
							);
				
				}
				
			break; 
			case "pendaftaran":
				$module	= "pendaftaran";
				// Input data
				// Cek data
				// Notifikasi Sukses
				// Kirim email
				// Cetak Konfirmasi pendaftaran
				
				// ambil data Jurusan
				$this->data['jurusan']		=	
							$this->mod_main->get_Data_Jurusan();
				$this->data['periode']					= 
							$this->mod_main->get_periode_pendaftaran();

				// status sukses
				$this->data['sukses']		= false;
				
				$data['web_title']				= $this->mod_general->get_web_setting('web_title');
				$data['nama_sekolah']			= $this->mod_general->get_web_setting('front_title');
				$data['moto_sekolah']			= $this->mod_general->get_web_setting('moto_sekolah');
				$data['alamat_sekolah']			= $this->mod_general->get_web_setting('alamat_sekolah');
				$data['telp_sekolah']			= $this->mod_general->get_web_setting('telp_sekolah');
				$data['email']					= $this->mod_general->get_web_setting('email');
				$data['logo']					= $this->mod_general->get_web_setting('web_logo');
				
				$data['nama_ketua_kampus']		= $this->mod_general->get_web_setting('nama_ketua_kampus');
				$data['nip_ketua_kampus']		= $this->mod_general->get_web_setting('nip_ketua_kampus');
				$data['jabatan_ketua_kampus']	= $this->mod_general->get_web_setting('jabatan_ketua_kampus');
				
				
				$nama 			= $this->input->post("nama");
				$jeniskelamin	= $this->input->post("jeniskelamin");
				$tempatlahir	= $this->input->post("tempatlahir");
				$tanggallahir	= $this->input->post("tanggallahir");
				$agama 			= $this->input->post("agama");
				$alamat			= $this->input->post("alamat");
				$telp		 	= $this->input->post("telp");
				$email 			= $this->input->post("email");
				$sekolahasal	= $this->input->post("sekolahasal");
				$jenjang		= $this->input->post("jenjang");
				$nem 			= $this->input->post("nem");
				$kodejurusan 	= $this->input->post("kodejurusan");

				$status_pekerjaan		= $this->input->post("status_pekerjaan");
				$status_perkawinan 		= $this->input->post("status_perkawinan");
				$pekerjaan_orang_tua 	= $this->input->post("pekerjaan_orang_tua");
				$alamat_orang_tua 		= $this->input->post("alamat_orang_tua");
				$pilih			 		= $this->input->post("pilih");
				$init 	= $this->input->post("init");
				if($init!="")
				{
					$cek = array(
						array("Nama"				, "0")
						, array("Jenis Kelamin"		, "0")
						, array("Tempat Lahir"      , "0")
						, array("Tanggal Lahir"		, "0")
						, array("Agama"				, "0")
						, array("Alamat"            , "0")
						, array("Telp"				, "0")
						, array("Email"				, "0")
						, array("Sekolah Asal"		, "0")
						, array("Jenjang"			, "0")
						, array("Nem"				, "0")
						, array("kode Jurusan"		, "0")
						, array("Status Pekerjaan"	, "0")
						, array("Status Perkawinan"	, "0")
						, array("Pekerjaan Orang Tua"	, "0")
						, array("Alamat Orang Tua"		, "0")
					);
					$ok				   = true;
					if($nama=="")
					{
						$cek[0][1] = "1";
						$ok		   = false;
					}
					if ($jeniskelamin=="")
					{
						$cek[1][1] = "1";
						$ok		   = false;
					}
					if ($tempatlahir=="")
					{
						$cek[2][1] = "1";
						$ok		   = false;
					}
					if ($tanggallahir=="")
					{
						$cek[3][1] = "1";
						$ok		   = false;
					}
					if ($agama=="")
					{
						$cek[4][1] = "1";
						$ok		   = false;
					}
					if ($alamat=="")
					{
						$cek[5][1] = "1";
						$ok		   = false;
					}
					if ($telp=="")
					{
						$cek[6][1] = "1";
						$ok		   = false;
					}
					if ($email=="")
					{
						$cek[7][1] = "1";
						$ok		   = false;
					}
					if ($sekolahasal=="")
					{
						$cek[8][1] = "1";
						$ok		   = false;
					}
					if ($jenjang=="")
					{
						$cek[9][1] = "1";
						$ok		   = false;
					}
					if ($nem=="")
					{
						$cek[10][1] = "1";
						$ok		   = false;
					}
					if ($kodejurusan=="")
					{
						$cek[11][1] = "1";
						$ok		   = false;
					}
					if ($status_pekerjaan=="")
					{
						$cek[12][1] = "1";
						$ok		   = false;
					}
					if ($status_perkawinan=="")
					{
						$cek[13][1] = "1";
						$ok		   = false;
					}
					if ($pekerjaan_orang_tua=="")
					{
						$cek[14][1] = "1";
						$ok		   = false;
					}
					if ($alamat_orang_tua=="")
					{
						$cek[15][1] = "1";
						$ok		   = false;
					}
					
					if($ok) 
					{
						$hasil =
						$this->mod_main->insert_pendaftaran
						(
								$nama,
								$jeniskelamin,
								$tempatlahir,
								$tanggallahir,
								$agama,
								$alamat,
								$telp,
								$email,
								$sekolahasal,
								$jenjang,
								$nem,
								$kodejurusan,
								$status_pekerjaan,
								$status_perkawinan,
								$pekerjaan_orang_tua,
								$alamat_orang_tua,
								$pilih,
								$this->data['periode'][0]['id']
						);
						$this->data['hasil']		= $hasil;
						$this->data['sukses']		= true;
					}
					$this->data["cek"] = $cek;
						
				}
				
			break; 
			case "galeri":
				$module	= "arsip_galeri";
					$this->data['galery']		=	$this->mod_main->get_galeri_full();
					$this->head["header_title"]		= $this->mod_general->get_web_setting('web_title')." - Arsip Galeri";
			break;
			case "detail_galeri":
				$module	= "detail_galeri";
					$this->data['galery']		=	$this->mod_main->get_detail_galeri($id_data);
					$this->head["header_title"]		= $this->mod_general->get_web_setting('web_title')." - Arsip Galeri";
			break;
			case "album":
				$module	= "detail_album";
					$this->data['album']		=	$this->mod_main->get_album($id_data);
					$this->head["header_title"]		= $this->mod_general->get_web_setting('web_title');
			break;
			case "berita":
				$module	= "arsip_berita";
					$start							= $this->uri->segment(2);
					if(!is_numeric($start))	$start	= 0;
					$this->data['arsip_berita']		= $this->mod_main->get_content_short("1", 10, $start);
					$this->data['start_berita']		= $start;
					$this->data['total_berita']		= $this->mod_main->get_content_total("1");
					$this->data['halaman_berita']	= 10;
					$this->data['judul_tag']		= "Arsip Berita";
					$this->head["header_title"]		= $this->mod_general->get_web_setting('web_title')." - Arsip Berita";
			break;
			case "info":
				$module	= "arsip_berita";
					$start							= $this->uri->segment(2);
					if(!is_numeric($start))	$start	= 0;
					$this->data['arsip_berita']		= $this->mod_main->get_content_short("2", 10, $start);
					$this->data['start_berita']		= $start;
					$this->data['total_berita']		= $this->mod_main->get_content_total("2");
					$this->data['halaman_berita']	= 10;
					$this->data['judul_tag']		= "Arsip Info";
					$this->head["header_title"]		= $this->mod_general->get_web_setting('web_title')." - Arsip Info";
			break;
			case "ekstra":
				$module	= "arsip_berita";
					$start							= $this->uri->segment(2);
					if(!is_numeric($start))	$start	= 0;
					$this->data['arsip_berita']		= $this->mod_main->get_content_short("3", 10, $start);
					$this->data['start_berita']		= $start;
					$this->data['total_berita']		= $this->mod_main->get_content_total("3");
					$this->data['halaman_berita']	= 10;
					$this->data['judul_tag']		= "Arsip Ekstrakurikuler";
					$this->head["header_title"]		= $this->mod_general->get_web_setting('web_title')." - Arsip Ekstrakurikuler";
			break;
			case "elearning":
				$module	= "arsip_elearning";
					$this->data['arsip_elearning']	= $this->mod_main->get_arsip_elearning();
					$this->head["header_title"]		= $this->mod_general->get_web_setting('web_title')." - Arsip E-Learning";
			break;
			case "pmb":
				$module	= "pmb";
					$this->data['arsip_elearning']	= $this->mod_main->get_arsip_elearning();
					$this->head["header_title"]		= $this->mod_general->get_web_setting('web_title')." - Arsip E-Learning";
			break;
			case "list_materi":
				$module	= "list_materi";
					$mp								= $this->uri->segment(2);
					$start							= $this->uri->segment(3);
					if(!is_numeric($start))	$start	= 0;
					$this->data['kode_mp']			= 	$mp;
					$this->data['nama_mp']			= $this->mod_main->get_nama_mp($mp);
					$this->data['arsip_materi']		= $this->mod_main->get_materi_short($mp, 10, $start);
					$this->data['start_materi']		= $start;
					$this->data['total_materi']		= $this->mod_main->get_materi_total($mp);
					$this->data['halaman_materi']	= 10;
					$this->data['judul_tag']		= "Arsip Materi";
					$this->head["header_title"]		= $this->mod_general->get_web_setting('web_title')." - Arsip Materi";
			break;
			case "support":
				$module	= "support";
//				$this->data['so_sekolah_kategori']	= $this->mod_main->get_support_online_sekolah_kategori();
				$this->data['so_sekolah']			= $this->mod_main->get_support_online_sekolah();
//				$this->data['so_inolabs']			= $this->mod_main->get_support_online_inolabs();
				$this->head["header_title"]			= "Support Online";
			break;
			case "maps":
				$module	= "maps";
				$theme_path	= $this->config->item('base_url')."application/views/template/";
				$theme_name	= $this->mod_general->get_template_name(
												$theme_path."theme_name.txt"
												);
				$this->data['lon']	= $this->mod_general->get_theme_setting(
									$theme_path.$theme_name."/school_setting.txt"
									,"LON"
				);
				$this->data['lat']	= $this->mod_general->get_theme_setting(
									$theme_path.$theme_name."/school_setting.txt"
									,"LAT"
				);
				$module	= "maps";
					$this->head["header_title"]		= "Maps";
			break;
			
			default:
				$this->data["learning"]			= $this->mod_main->get_learning_short(6);
				$this->data["galeri"]			= $this->mod_main->get_galery_short(3);
				$this->data["sidebar_calender"]	= $this->mod_main->get_current_callender();
				$this->data['sys_page']			= "main";		
				$this->head['sys_page']			= "main";		
				$module	= "main";
			break;
		}
		
		$this->data['module']	=	$module;		
		$this->head['module']	=	$module;		
		$this->load->view($theme_path_view."sys_header", array("data"=>$this->head));
		$this->load->view($theme_path_view."sys_content", array("data"=>$this->data));
		$this->load->view($theme_path_view."sys_footer", $this->data);	
	}
	
	function cekLogin()
	{		
		$this->head['loginAkses'] = "";
		$kategori	= $this->session->userdata("akses");
		$loginId	= $this->session->userdata("userId");
		if($kategori=="dosen")
		{
			$this->head['loginAkses'] = "dosen";
			$this->head['loginInfo'] = $this->mod_main->getIdentitasGuru($loginId);
		}
		elseif($kategori=="siswa")
		{
			$this->head['loginAkses'] = "siswa";
			$this->head['loginInfo'] = $this->mod_main->getIdentitasSiswa($loginId);
		}
	}
	
	function loginAction()
	{
		$this->load->model("engine/pass_word");
		$u_name	= $this->input->post("u_name");
		$u_pass	= $this->input->post("u_pass");
		$u_kat	= $this->input->post("u_kat");
		$u_init	= $this->input->post("u_init");
		$this->data['msg_login'] = "";
		if($u_init=="login")
		{
			if($u_kat=="Guru")
			{
				$sql	= "SELECT kode_dosen, `pass` FROM tbl_dosen
									WHERE
									kode_dosen 	= '$u_name'
									"
				."AND
									`status`	= '1'";
				$rec	= $this->db->query($sql);
				$count	= $rec->num_rows();
				$res	= $rec->result_array();
				if($count>0)
				{
					if($this->pass_word->decryptPass($res[0]['pass'])==$u_pass)
					{
					$this->session->sess_destroy();
					$this->session->set_userdata(
										array(
											'userId' 	=> $res[0]['kode_dosen']
											, 'akses'	=> "dosen"
										)
									);
					}
					else
						$this->data['msg_login'] = "Password tidak cocok";
				}
				else
					$this->data['msg_login'] = "User tidak ditemukan";
			}
			elseif($u_kat=="Siswa")
			{
				$sql	= "SELECT nim, pass FROM tbl_siswa
									WHERE
									nim	= '$u_name'";
				$rec	= $this->db->query($sql);
				$count	= $rec->num_rows();
				$res	= $rec->result_array();
//					echo $res[0]['pass']."ini pass di db<br>";
//					echo $this->pass_word->decryptPass($res[0]['pass'])."ini pass setelah decrypt<br>";
//					echo $u_pass."ini pass send<br>";
				if($count>0)
				{
					if($this->pass_word->decryptPass($res[0]['pass'])==$u_pass)
					{
						$this->session->sess_destroy();
						$this->session->set_userdata(
											array(
												'userId' 	=> $res[0]['nim']
												, 'akses'	=> "siswa"
											)
										);
					}
					else
						$this->data['msg_login'] = "Password tidak cocok";
				}
				else
					$this->data['msg_login'] = "User tidak ditemukan";
			}
		}
		else
		{
			$param = $this->uri->segment(1);
			if($param=="logout")
			{
				$this->session->set_userdata(
											array(
												'userId' 	=> ""
												, 'akses'	=> ""
											)
										);
				$this->session->sess_destroy();
			}
		}
		$this->head['msg_login'] = $this->data['msg_login'];
	}
}
?>